import { isDevMode } from '../env/env';
interface userInfo {
  // eslint-disable-next-line camelcase
  tenantCode?: string;
  userId?: string;
  userName?: string;
  tokenType?: string;
  appType?: string;
  token?: string;
  localToken?: string;
  consoleDomain?: string;
}

/**
 * 解析 URL 中的参数 userInfo, token
 * @param rawUrl 原始 URL
 * @returns 用户信息
 */
export function parseUrl(rawUrl: string): userInfo {
  const url = decodeURIComponent(rawUrl);
  const tmp = url.split('?')[1];
  const consoleDomain = isDevMode()
    ? window.location.href.split('?')[0].substring(0, window.location.href.lastIndexOf('/'))
    : url.split('?')[0].substring(0, url.lastIndexOf('/'));
  if (!tmp) {
    return {};
  }
  const paramsList = tmp.split('&');
  const userInfo: Record<string, string> = {};
  for (let i = 0; i < paramsList.length; i++) {
    const [key, value] = paramsList[i].split('=');
    userInfo[key] = value;
  }
  userInfo.tokenType = userInfo.app_type === '3' ? 'session' : 'local';
  // 根据app_type判断应用类型，业务应用使用session存type, appType:{1:平台侧控制台应用}
  userInfo.appType = Number(userInfo.app_type) as 1 | 2 | 3;
  userInfo.consoleDomain = consoleDomain;
  delete userInfo.app_type;
  return userInfo;
}
export function replaceHistoryState(rawUrl: string) {
  history.replaceState(null, '', rawUrl);
}
